<?php
class Mclog {

    private $v = null;
    private $basePath = null;
    public $weburl = null;
    public $themeurl = null;

    public function __construct() {
        $this->v = mc_class( 'Template' );
        $this->v->init( APP_PATH . '/themes/' . APP_MODULE . '/', APP_TPL . APP_MODULE . '/' );
        $this->weburl = mc_setting( 'weburl' ) ? mc_setting( 'weburl' ) : mc_blog_url();
        $this->themeurl = "{$this->weburl}/themes/" . APP_MODULE;
        $this->basePath = mc_base();
    }

    /**
     * 返回编译后模板文件
     * @param file          要编译的模板文件
     * @param refresh       是否重新编译模板
     */
    public function display( $file, $refresh = true ) {
        return $this->v->display( $file, $refresh );
    }

    /**
     * url跳转
     * @param url         跳转地址
     */
    public function redirect( $url ) {
        header( "Location: {$url}" );
        exit;
    }

    /*
     *  URL模式的构建函数
     *
     * @param controller    控制器名称
     * @param action        动作名称
     * @param args          传递的参数，数组形式
     * @param anchor        跳转锚点
     */
    public function mc_url( $controller = null, $action = 'index', $args = null, $anchor = null ) {

        // 使用扩展点
        $rewrite = intval( mc_setting( 'rewrite' ) );
        if ( $rewrite == 2 ) {
            if ( $launch = mc_launch( "rewrite", array( 'c' => $controller, 'a' => $action, 'args' => $args, 'anchor' => $anchor ), true ) ){
                return $launch;
            } 
        }

        // 使用path_info方式
        if ( $rewrite == 1 ) {
            $url = "{$this->basePath}/{$controller}/{$action}";
            if ( null != $args ) foreach ( $args as $key => $arg ) $url .= "/{$key}/{$arg}";
        } else {
            $url = "{$this->basePath}?c={$controller}&a={$action}";
            if ( null != $args ) foreach ( $args as $key => $arg ) $url .= "&{$key}={$arg}";
        }

        if ( null != $anchor ) $url .= "#" . $anchor;
        return $this->weburl . $url;
    }

    /**
     * 魔术函数，执行控制器扩展类的自动加载及使用
     */
    public function __call( $name, $args ) {
        return mc_class( $name )->__input( $this, $args );
    }
}